import { nodePath } from './path.node';
import { CRLF } from './constant';
import { Config } from './../entrance';
import { joinPath, toPosix } from "./path.node";
import { toCamelStyle, toUnderLineStyle } from "./string";
import { nodeFileSystem } from "./fs.node";

function writeTypescriptFile(folder: string, componentName: string, config: Config) {
    const shortName = componentName + ".ts";
    const file = joinPath(folder, shortName);
    const tpl = `import "./${componentName}.scss";
//
import { Vue, Component } from "vue-property-decorator";

@Component({
    template: require("./${componentName}.html"),
})
export class ${componentName} extends Vue {
}
`;
    writeFile(file, tpl, shortName, config);
}


function writeFile(file: string, tpl: string, shortName: string, config: Config) {
    if (nodeFileSystem.existsSync(file) && !config.rewrite) {
        console.log("****[ exist target file ]*********error: log start>>>>[ exist target file ]>>>>>>>>>>>>>");
        console.log("write file error: [ exist target file ] -> " + shortName);
        console.log(file);
        console.log(shortName + "已存在");
        console.log("<<<<[ exist target file ]<<<<<<<error: log end********[ exist target file ]**********");
        return;
    }
    nodeFileSystem.writeFile(file, tpl, "utf8", (err) => {
        if (err) {
            console.log("*************error: log start>>>>>>>>>>>>>>>>>");
            console.log("write file error: " + shortName);
            console.log(file);
            console.error(err);
            console.log("<<<<<<<<<<<error: log end******************");
        } else {
            console.log("-------------success: log start>>>>>>>>>>>>>>>>>");
            console.log("write file success: " + shortName);
            console.log(file);
            console.log("<<<<<<<<<<<success: log end---------------");
        }
    });
}

function writeScssFile(folder: string, componentName: string, config: Config) {
    const shortName = componentName + ".scss";
    const file = joinPath(folder, shortName);
    let tpl = config.initStyles.map(style => {
        const styleFile = joinPath(config.root, config.codeRoot, style);
        return `@import "${toPosix(nodePath.relative(nodePath.dirname(file), styleFile)).replace(/\.scss$/, "")}";`;
    }).join(CRLF);
    tpl += `
.c-${toUnderLineStyle(componentName)} {}
`;
    writeFile(file, tpl, shortName, config);
}

function writeHTMLFile(folder: string, componentName: string, config: Config) {
    const shortName = componentName + ".html";
    const file = joinPath(folder, shortName);
    const tpl = `<${config.rootTag} class="c-${toUnderLineStyle(componentName)}"></${config.rootTag}>`;
    writeFile(file, tpl, shortName, config);
}

export function writeVueTSFile(folder: string, componentName: string, config: Config) {
    const camelName = toCamelStyle(componentName);
    writeTypescriptFile(folder, camelName, config);
    writeScssFile(folder, camelName, config);
    writeHTMLFile(folder, camelName, config);
}